#!/usr/bin/envpython
#-*-coding:utf-8-*-
import sys,os,re,json
sys.path.append('../manager/')
os.environ['DJANGO_SETTINGS_MODULE']='manager.settings'
from manager import settings
import django
django.setup()
from san import models
from django.db.models import Q
# obj = models.Storage.objects.get(id = 10)
# # print(obj.storagehost_set.all())
# # for i in obj.storagehost_set.all():
# #     # print(i.hostname)
# #     # print(i.wwn_set.all())
# #     for w in i.wwn_set.all():
# #         if w.Sanport_wwn:
# #             # print(w.Sanport_wwn.san.ip)
# #             print(w.Sanport_wwn.rx)
# #
# for i in obj.storageport_set.all():
#     print(i)
# data = {"prodname":"Brocade"}
# sanobj = models.San.objects.all().filter(prodname = "Brocade")
# sanoj =models.San.objects.filter(prodname=u"惠普")EMC
# print(sanobj)

# a = models.San.objects.filter(Q(prodname = "Brocade") | Q(prodname=u"惠普"))
# print(a)
# st_log = models.Doment.objects.filter(Q(storage__ST_prodname = "富士通")| Q(storage__ST_prodname = "日志") | Q(storage__ST_prodname = "华为") | Q(storage__ST_prodname = ""))
# print(st_log)

# st_log = models.San.objects.filter(
#     Q(prodname="Brocade") | Q(prodname="惠普"))
# for i in st_log:
#     print(i.id)
# from script.fsjinfo import fsjinfo
# objname = models.Doment.objects.filter(storage__ST_sn="4541445007").first()
# a = fsjinfo(objname.text)
# hostgroup = a.Hostgroup()
# for i in hostgroup[0]:
#     print(i)
# portgroup = a.portgroup()
# for i in portgroup[1]:
#     print(i,portgroup[1][i])
#
# from script.fsttodata import Fsttodata
# id = models.Storage.objects.get(ST_sn="4541635001")
# print(id)
# objname = models.Doment.objects.filter(storage__ST_sn="4541635001").first()
# a = Fsttodata(id,objname.text)

# wwn = models.Wwn.objects.filter(wwn="10:00:00:05:1e:fd:98:8f")
# print(wwn[0].Sanport_wwn.san)
def Re(res,total,id = "1"):
    if id == "1":
        data = re.findall(str(res),total)
    if id == "2":
        data = re.findall(str(res),total,re.S)
    if id == "3":
        data = re.findall(str(res),total,re.S)
    return data
# models.Storagehost.objects.all().delete(STLUNgrou)
# #
# from storagedata.models import STLUNgroup
# print(STLUNgroup.objects.filter(lid=2))
#富士通设置
# from storagedata.fsttodata import Fsttodata
# obj = models.Storage.objects.filter(ST_sn="4541446002").first()
# objtext = models.Doment.objects.filter(storage__ST_sn="4541446002").order_by("-dotime").first()
# from script.fsjinfo import fsjinfo
# a = fsjinfo(objtext.text)
# a.lun()
#日立
# from storagedata import models
# from san.models import Storage,Doment
# obj =Storage.objects.filter(ST_sn="51546").first()
# objtext = Doment.objects.filter(storage__ST_sn="51546").order_by("-dotime").first()
# from storagedata.hitachiscript import hitachi_to_data
# hitachi_to_data(obj.id,objtext.text)
#华为测试
# from storagedata import models
# from san.models import Storage,Doment
# obj  = Storage.objects.get(ST_sn="210235982610GB000031")
# objtext = Doment.objects.filter(storage_id =obj.id).order_by("-dotime")
# from script.huaweistorage import Huawei
# a = Huawei(objtext[0].text)
# print(a.maping())
#
# from storagedata import models
# obj = models.STMAP.objects.get(id= 350)
# obj.Notes = "ceshi"
# obj.save*()
# from san import models
# obj = models.Doment.objects.filter(san__ip="10.226.24.124").order_by("-dotime").first()
# from san.sanbokescript import Sanmessages,sanporttodata
#
# sanporttodata(11,obj.text)
# from storagedata.models import STLUN,STLUNgroup
#
# #添加存储id，
# def FST_lunnum(id):
#     group = STLUN.objects.filter(storage=1)
#     lunuse = 0
#     lunkeyong = 0
#     lunkeyongnum = 0
#     for i in group:
#         lunkeyong += float(i.lunsize)
#         if i.stlungroup_set.all():
#             lunuse += float(i.lunsize)
#         else:
#             lunkeyongnum += 1
#     return [lunuse, lunkeyong, lunkeyongnum]
# from work.models import Work
# # obj = Work.objects.filter(Q(WO_fishtime__year=2017) and Q(WO_fishtime__month=7))
# import datetime
#
# na = Work.objects.filter(WO_fishtime__year=2017)
# for i in na:
#     a = i.WO_fishtime
#
#     print(a.month)
# print(na)

#cisco光交
# from san.ciscoscript import CISCOMDS
# def Cisco_to_data(id):
#     obj = models.Doment.objects.filter(san_id = id).order_by("-dotime").first()
#     data_shuju = CISCOMDS(obj.text)
#     mds_port = data_shuju.mds_port()
#
#     #主端口更新
#     for i in mds_port:
#         data ={
#             "slot":int(i[0]),
#             "port":int(i[1]),
#             "speed":i[3],
#             "type":i[2],
#             "san_id":id,
#         }
#         obj = models.Sanport.objects.filter(slot=i[0], port=i[1], san_id=id).first()
#         if not obj:
#             obj = models.Sanport.objects.create(**data)
#         else:
#             obj.speed = i[3]
#             obj.type = i[2]
#         mds_portmess = data_shuju.mds_portmess()
#         for k in mds_portmess:
#             if i[0] == k[0] and i[1] == k[1]:
#                 obj.tx = k[3]
#                 obj.rx = k[2]
#                 obj.enc_in = k[4]
#                 obj.crc_err = k[5]
#         mds_right = data_shuju.mds_right()
#         for z in mds_right:
#             if i[0] == z[0] and i[1] == z[1]:
#                 obj.RX_Power = z[2]
#                 obj.TX_Power = z[3]
#
#         mds_wwn = data_shuju.mds_wwn()
#         for w in mds_wwn:
#             if i[0] == w[0] and i[1] == w[1]:
#                 wwn = models.Wwn.objects.update_or_create(wwn=w[3])
#                 wwn[0].Sanport_wwn = obj
#                 wwn[0].save()
#         obj.save()
# Cisco_to_data(13)



# from san.models import Host
# try:
#     obj = Host.objects.get(sn="",ip="10.180.213.38")
#     print(obj)
# except Exception as e:
#     print("jixuyunxing")
# from work.models import Work
# obj = Work.objects.get(id = 1)
# print(obj.WO_fishtime)
# a = "2017-7-24 7:24:0"
# obj.WO_fishtime = a
# print(obj.WO_fishtime)
# obj.save()

# from storagedata.models import STMAP
# def rili_lunnum(id):
#     group = STMAP.objects.filter(storage=id)
#     for i in group:
#         maplun = 0
#         for k in i.manylun.all():
#             maplun += float(k.lunsize)
#         i.totalcap = round(maplun,1)
#         i.save()
# rili_lunnum(12)


# from storagedata.models import STMAP
# from san.models import Storage
# def rili_lunnum(id):
#     group = STMAP.objects.filter(storage=id)
#     total = 0
#     for i in group:
#         use = 0
#         for k in i.manylungroup.all():
#             for z in k.lun.all():
#                 print(z.lunsize)
#                 use += float(z.lunsize)
#         i.totalcap = use
#         total += use
#         i.save()
#     group2 = Storage.objects.get(id = id)
#
#
# rili_lunnum(1)

#
#
# from san.models import Wwn
# Wwn.objects.get(wwn = "10:00:00:90:fa:ef:46:a6")
from san.models import Sanport,Doment,Sanlog_clean
# from san.brocade_caiji import Borde_huitu
# Borde_huitu("5")

from san.models import Sanlog_clean
# a  = Sanlog_clean.objects.filter(enc_in__gt = 0,enc_out__gt = 0)
# print(len(a))
# from san.models import Sanport,Storage,Doment
# obj = Doment.objects.get(id = 119)
# from script.huaweistorage import Huawei
# a = Huawei(obj.text)
# for i in a.disk():
#     print(i)
# print(len(a.disk()))
# obj = models.San.objects.filter(prodname = "Brocade")
# for i in obj:
#     port = models.Sanport.objects.filter(san=i)
#     for p in port:
#         obj = Sanlog_clean.objects.filter(San_portid = p).order_by("-log_time")
#         new = obj[0]
#         sec = obj[1]
#         last = obj.last()
#         if obj[0].enc_in != obj[1].enc_in or obj[0].enc_in !=obj.last().enc_in:
#             print(p.slot,p.port,p.san.ip)
#
from san.models import Storage
from storagedata.models import STLUNgroup,STLUN
# def FST_lunnum(id):
#     # group = STMAP.objects.filter(storage=id)
#     total = 0
#     # for i in group:
#     #     use = 0
#     #     for k in i.manylungroup.all():
#     #         for z in k.lun.all():
#     #             use += float(z.lunsize)
#     #     i.totalcap = use
#     #     total += use
#     #     i.save()
#     lungroup = STLUNgroup.objects.filter(storage=id)
#     for i in lungroup:
#         use = 0
#         print(i.lungroupname,"lun组")
#         if i.stmap_set.all():
#             for z in i.lun.all():
#                 print(z)
#                 use += float(z.lunsize)
#
#         else:
#             print("有空的lun租", i.lungroupname)
#         total += use
#
#     print(total)
#     # group2 = Storage.objects.get(id = id)
#     # group2.ST_usecap = total
#     # group2.save()
#     return True
# FST_lunnum(6)
# from django.db.models import Q
# con = Q()
# q1 = Q()
# q1.connector = 'OR'
# q1.children.append(('rx__gt', 5))
# q1.children.append(('tx__gt', 5))
# q2 = Q()
# q2.connector = 'and'
# q2.children.append(('san__prodname', "CISCO"))
# con.add(q1, 'AND')
# con.add(q2, 'AND')
#
#
# # obj2 = models.Sanport.objects.filter(san__prodname="CISCO",rx__gt=5,tx__gt = 5)
# obj2 = models.Sanport.objects.filter(con).values("id","index")
# print(obj2[0].wwn_set)

#计算真实的可用容量
# from storagedata.models import STLUN,RAIDgroup
# obj = RAIDgroup.objects.filter(storage_id="10")
# a = 0
# for i in obj:
#     a += float(i.raidsize)
# print(a)

# from storagedata.models import STMAP
# a = ["HGT-X5-Citrix-POOL(NWYpool1","HGT-X5-Citrix-POOL(NWYpool2)","citrix_DMZ03","citrix","HGT-X4-vmware-Cluster-I0910-J0910","HGT-X4-vmware-Cluster-I1112-J1112","管理集群K8","HGT-X4-vmware-Cluster-I1314-J1314","HGT-X4-vmware-Cluster-I0506-J0506","HGT-X4-vmware-Cluster-I0304-J0304","HGT-X4-vmware-Cluster-I0708-J0708","vmware-cluster1","HGT-X4-vmware-cluster1","vmwareI03-04集群","vmware集群业务I0304","vmware集群业务 vmware09-10","5HGT-X4-vmware-Cluster-I0506-I0506","HGT-X4-vmware-Cluster-I0708-I0708","HGT-X5-Citrix-POOL(NWYpool1）","HGT-X5-Citrix-POOL(NWYpool2）","vmware(CRM04集群)","思杰虚拟化(kefugroup)","citrix_cluster01(5楼新实施)","citrix_cluster02(5楼新实施)","citrix_ QD&BOMC虚拟化","二维码数据库","金钥匙平台数据库","我信数据库","电商平台数据库(DSP)","“金钥匙平台应用”","CRM 测试数据库","结算系统","结算计费","买平台离线查询库","网厅测试数据库","计费业务系统","解码系统","无纸化数据库","网服协同办公文本","经分","电商平台数据库","思杰虚拟化jxcmcc","东软云平台数据库","计费测试","计费MDB云化","计费解码域","备份系统","云桌面","SDN管理平台","BPM管理平台","账务数据库","测试数据库","网服协同办公","账务自动化测试","esop系统","云平台数据库一","云平台数据库二","云平台数据库三","云平台数据库四","GIS数据库","4A审计数据库","4A数据库","SMP数据库","小单元系统","华为云桌面","pon激活接入系统","传输端对端","卡卷中心","卡卷中心数据库","roracle数据库","mis系统数据库","安全评估平台"]
# for i in a:
#     a= STMAP.objects.filter(app = i)
#     b = 0
#     for z in a:
#         b += float(z.totalcap)
#     print(i,"    ",b)

#
# from storagedata.storage_num import FST_lunnum
# obj = models.Storage.objects.get(id = 20)
#
# FST_lunnum(obj.id)
from work.models import Work
# obj = Work.objects.select_related("WO_storage").all().order_by("-WO_time")
# for i in obj:
#     print(i.WO_storage.id)
# from storagedata.models import STMAP,STHOST
# obj = STMAP.objects.filter(storage_id= "12")
# for i in obj:
#     k = i.mapname
#     obj2 = STHOST.objects.filter(hostname=k)
#     num =[]
#     for zz in obj2:
#         num.append(zz.Notes)
#     num2 = str(set(num))
#     i.Notes = num2
#     i.save()


# st_log = models.Doment.objects.values("dotime","san__ip").filter(
#     Q(storage__ST_prodname="富士通") | Q(storage__ST_prodname="日立") | Q(storage__ST_prodname="华为") | Q(
#         storage__ST_prodname="EMC"))
# for i in st_log:
#     print(i["san__ip"])
# obj = models.Sanport.objects.filter(rx__contains="m")
# from django.db.models import Q
#
# con = Q()
# q1 = Q()
# q1.connector = 'OR'
# q1.children.append(('rx__gt', 5))
# q1.children.append(('tx__gt', 5))
# q2 = Q()
# q2.connector = 'and'
# q2.children.append(('san__prodname', "CISCO"))
# con.add(q1, 'AND')
# con.add(q2, 'AND')
# obj2 = models.Sanport.objects.values("id","wwn__wwn","san__prodect", "san__ip", "wwn__host_wwn", "wwn__host_wwn__app",
#                                          "wwn__host_wwn__ip", "wwn__storage_wwn__ST_prodect",
#                                          "wwn__storage_wwn__ST_ip").filter(con)
# for i in obj2:
#     print(i)

#
#
# obj = models.Sanport.objects.values("id","slot","port","wwn__wwn").filter(san_id = 12)
# for i in obj:
#     print(i)

#
# from scriptd.tasks import Task
# a = Task()
# a.start("运行起来")
# import time
# time.sleep(3)
# a.end()
# domxinxi = Doment.objects.filter(san_id = 3).order_by("-dotime").first()
# print(domxinxi.dotime)

from storagedata.models import STHOST,STMAP
from san.models import Wwn

# obj = STHOST.objects.values("id", "hostname", "sthostgroup__hostgroupname",
#                             "wwn__wwn","wwn__Sanport_wwn__port", "wwn__Sanport_wwn__slot",
#                             "wwn__Sanport_wwn","wwn__Sanport_wwn__tx", "wwn__Sanport_wwn__rx", "wwn__host_wwn",
#                             "wwn__host_wwn__app", "wwn__host__ip").filter(storage_id=12)
obj = STHOST.objects.values("id", "hostname", "sthostgroup__hostgroupname","wwn__wwn","wwn__Sanport_wwn__san__ip","wwn__Sanport_wwn__slot",
                            "wwn__Sanport_wwn__port","wwn__Sanport_wwn__rx","wwn__Sanport_wwn__tx",
                            "wwn__host_wwn__app","wwn__host_wwn__ip").filter(storage_id=12)

for i in obj:
    print(i)